package com.easy.facilitaterecycleadapter.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.facilitaterecycleadapter.R;

import java.util.List;

/**
 * Created by Sigthp-P-Department on 2018/5/7.
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHodler> {

    public final Context mContext;
    public final List<T> mList;
    public final LayoutInflater mInflater;
    private  int mLayoutId;
    private  MultiTypeSupport multiTypeSupport;


    public BaseAdapter(List<T> list, Context context,int layoutId){
        this.mContext=context;
        this.mList=list;
        this.mLayoutId=layoutId;
        mInflater = LayoutInflater.from(mContext);
    }
    public BaseAdapter(List<T> list, Context context,MultiTypeSupport multiTypeSupport){
        this(list,context,-1);
        this.multiTypeSupport=multiTypeSupport;
    }

    @Override
    public BaseViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        // 多布局支持
        if (multiTypeSupport != null) {
            mLayoutId = viewType;
        }
        View view = mInflater.inflate(mLayoutId, parent, false);
        BaseViewHodler viewHodler=new BaseViewHodler(view);
        return viewHodler;
    }

    @Override
    public int getItemViewType(int position) {
        // 多布局支持
        if (multiTypeSupport != null) {
            return multiTypeSupport.getLayoutId(mList.get(position), position);
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(BaseViewHodler holder, int position) {
        bindData(holder,position);
    }

    public abstract void bindData(BaseViewHodler holder, int position);

    @Override
    public int getItemCount() {
        return mList.size();
    }
}
